In [1]:
#import required packages
import numpy as np
import pandas as pd
import nltk
import tamil
import matplotlib.pyplot as plt
In [2]:
pd_kural = pd.read_csv('F:/analytics/analytics_experiments_personal/kural/kural_dat_utf8.txt',index_col=0,sep=',',header='infer')
In [3]:
kural_list = list(pd_kural["kural"])
In [4]:
ng_test = nltk.ngrams(kural_list[0].split(" "),2)
for grams in ng_test : print(grams)
('அகர', 'முதல')
('முதல', 'எழுத்தெல்லாம்')
('எழுத்தெல்லாம்', 'ஆதி')
('ஆதி', 'பகவன்')
('பகவன்', 'முதற்றே')
('முதற்றே', 'உலகு')
In [5]:
tri_array = []
for i in range(0,3) : 
    ng = nltk.ngrams(kural_list[i].split(" "),3)
    for grams in ng : 
        tri_array.append(grams)
print(tri_array)
[('அகர', 'முதல', 'எழுத்தெல்லாம்'), ('முதல', 'எழுத்தெல்லாம்', 'ஆதி'), ('எழுத்தெல்லாம்', 'ஆதி', 'பகவன்'), ('ஆதி', 'பகவன்', 'முதற்றே'), ('பகவன்', 'முதற்றே', 'உலகு'), ('கற்றதனால்', 'ஆய', 'பயனென்கொல்'), ('ஆய', 'பயனென்கொல்', 'வாலறிவன்'), ('பயனென்கொல்', 'வாலறிவன்', 'நற்றாள்'), ('வாலறிவன்', 'நற்றாள்', 'தொழாஅர்'), ('நற்றாள்', 'தொழாஅர்', 'எனின்'), ('மலர்மிசை', 'ஏகினான்', 'மாணடி'), ('ஏகினான்', 'மாணடி', 'சேர்ந்தார்'), ('மாணடி', 'சேர்ந்தார்', 'நிலமிசை'), ('சேர்ந்தார்', 'நிலமிசை', 'நீடுவாழ்'), ('நிலமிசை', 'நீடுவாழ்', 'வார்')]
In [6]:
kural_vocab = []
for i in range(len(kural_list)) :
    k = kural_list[i].split()
    kural_vocab.extend(k)
In [7]:
wc=[]
for i in range(len(kural_vocab)) :
    c=0
    for j in range(len(kural_list)) :
        c = c + kural_list[j].count(kural_vocab[i])
    wc.append([kural_vocab[i],c])
In [8]:
len(wc)
Out[8]:
9391
In [9]:
pd_unicount = pd.DataFrame(data=wc)
In [10]:
pd_unicount.head()
pd_unicount.columns = ["word","count"]
pd_unicount = pd_unicount.drop_duplicates()
pd_unicount.head()
Out[10]:
word count
0 அகர 1
1 முதல 7
2 எழுத்தெல்லாம் 1
3 ஆதி 2
4 பகவன் 1
In [11]:
pd_unicount.describe()
Out[11]:
count
count 6374.000000
mean 3.761374
std 38.482619
min 1.000000
25% 1.000000
50% 1.000000
75% 2.000000
max 1912.000000
In [12]:
pd_unicount.query('count==1912')
Out[12]:
word count
389 ம் 1912
In [13]:
pd_unicount = pd_unicount[pd_unicount.word != "ம்"]
pd_unicount.describe()
Out[13]:
count
count 6373.000000
mean 3.461949
std 30.159406
min 1.000000
25% 1.000000
50% 1.000000
75% 2.000000
max 1717.000000
In [14]:
pd_unicount.query('count==1717')
Out[14]:
word count
2894 1717
In [15]:
pd_unicount = pd_unicount[pd_unicount.word != "ட"]
pd_unicount.describe()
Out[15]:
count
count 6372.000000
mean 3.193032
std 21.184633
min 1.000000
25% 1.000000
50% 1.000000
75% 2.000000
max 1314.000000
In [16]:
pd_unicount.query('count==1314')
Out[16]:
word count
5623 ர் 1314
In [17]:
pd_unicount = pd_unicount[pd_unicount.word != "ர்"]
pd_unicount.describe()
Out[17]:
count
count 6371.000000
mean 2.987286
std 13.382127
min 1.000000
25% 1.000000
50% 1.000000
75% 2.000000
max 736.000000
In [18]:
pd_unicount.query('count==736')
Out[18]:
word count
1725 கு 736
In [19]:
pd_unicount = pd_unicount[pd_unicount.word != "கு"]
pd_unicount.describe()
Out[19]:
count
count 6370.000000
mean 2.872214
std 9.733100
min 1.000000
25% 1.000000
50% 1.000000
75% 2.000000
max 331.000000
In [20]:
pd_unicount.query('count==331')
Out[20]:
word count
2463 க்கு 331
In [21]:
pd_unicount = pd_unicount[pd_unicount.word != "க்கு"]
pd_unicount.describe()
Out[21]:
count
count 6369.000000
mean 2.820694
std 8.822574
min 1.000000
25% 1.000000
50% 1.000000
75% 2.000000
max 233.000000
In [22]:
pd_unicount.query('count==233')
Out[22]:
word count
1039 கண் 233
In [23]:
pd_unisort=pd_unicount.sort_values(by='count',ascending=0)
pd_unisort.head()
Out[23]:
word count
1039 கண் 233
333 கி 199
2777 டும் 186
1494 என் 172
27 இல 154
In [24]:
test = pd_unisort.query('count>10 and count <=500')
In [25]:
test_x=list(test["word"])
test_y=list(test["count"])
test_s=list(test["count"])
In [26]:
import plotly as py
import plotly.graph_objs as go
py.offline.init_notebook_mode()

trace0 = go.Scatter(
    x= test_x,
    y= test_y,
    mode='markers',
    marker=dict(
        size=test_s,
    )
)

data = [trace0]
py.offline.iplot(data, filename='bubblechart-size')
In [27]:
uyir = ['அ','ஆ','இ','ஈ','உ','ஊ','எ','ஏ','ஐ','ஒ','ஓ','ஔ']
len(uyir)
Out[27]:
12
In [28]:
mey = ['க்','ங்','ச்','ஞ்','ட்','ண்','த்','ந்','ப்','ம்','ய்','ர்','ல்','வ்','ழ்','ள்','ற்','ன்']
len(mey)
Out[28]:
18
In [29]:
uyirmey = ['க','கா','கி','கீ','கு','கூ','கெ','கே','கை','கொ','கோ','கௌ','ங','ஙா','ஙி','ஙீ','ஙு','ஙூ','ஙெ','ஙே','ஙை','ஙொ','ஙோ','ஙௌ','ச','சா','சி','சீ','சு','சூ','செ','சே','சை','சொ','சோ','சௌ','ஞ','ஞா','ஞி','ஞீ','ஞு','ஞூ','ஞெ','ஞே','ஞை','ஞொ','ஞோ','ஞௌ','ட','டா','டி','டீ','டு','டூ','டெ','டே','டை','டொ','டோ','டௌ','ண','ணா','ணி','ணீ','ணு','ணூ','ணெ','ணே','ணை','ணொ','ணோ','ணௌ','த','தா','தி','தீ','து','தூ','தெ','தே','தை','தொ','தோ','தௌ','ந','நா','நி','நீ','நு','நூ','நெ','நே','நை','நொ','நோ','நௌ','ப','பா','பி','பீ','பு','பூ','பெ','பே','பை','பொ','போ','பௌ','ம','மா','மி','மீ','மு','மூ','மெ','மே','மை','மொ','மோ','மௌ','ய','யா','யி','யீ','யு','யூ','யெ','யே','யை','யொ','யோ','யௌ','ர','ரா','ரி','ரீ','ரு','ரூ','ரெ','ரே','ரை','ரொ','ரோ','ரௌ','ல','லா','லி','லீ','லு','லூ','லெ','லே','லை','லொ','லோ','லௌ','வ','வா','வி','வீ','வு','வூ','வெ','வே','வை','வொ','வோ','வௌ','ழ','ழா','ழி','ழீ','ழு','ழூ','ழெ','ழே','ழை','ழொ','ழோ','ழௌ','ள','ளா','ளி','ளீ','ளு','ளூ','ளெ','ளே','ளை','ளொ','ளோ','ளௌ','ற','றா','றி','றீ','று','றூ','றெ','றே','றை','றொ','றோ','றௌ','ன','னா','னி','னீ','னு','னூ','னெ','னே','னை','னொ','னோ','னௌ']
len(uyirmey)
Out[29]:
216
In [30]:
len(kural_list[0])
Out[30]:
45
In [31]:
tamil.utf8.get_letters(kural_list[0])
Out[31]:
['அ',
 'க',
 'ர',
 ' ',
 'மு',
 'த',
 'ல',
 ' ',
 'எ',
 'ழு',
 'த்',
 'தெ',
 'ல்',
 'லா',
 'ம்',
 ' ',
 'ஆ',
 'தி',
 ' ',
 'ப',
 'க',
 'வ',
 'ன்',
 ' ',
 'மு',
 'த',
 'ற்',
 'றே',
 ' ',
 'உ',
 'ல',
 'கு']
In [32]:
tamil.utf8.get_letters(kural_list[0][1])[0] == mey[0]
Out[32]:
False
In [33]:
uyir_count = []
for u in range(len(uyir)) :
    count = 0
    for k in range(len(kural_list)) :
        for l in range(len(kural_list[k])) :
            if tamil.utf8.get_letters(kural_list[k][l])[0] == uyir[u] :
                count = count + 1
    uyir_count.append([uyir[u],count])
print(uyir_count)
[['அ', 834], ['ஆ', 204], ['இ', 670], ['ஈ', 49], ['உ', 584], ['ஊ', 71], ['எ', 451], ['ஏ', 65], ['ஐ', 19], ['ஒ', 201], ['ஓ', 32], ['ஔ', 0]]
In [34]:
mey_count = []
for m in range(len(mey)):
    all_count = 0
    for k in range(len(kural_list)):
        this_count = kural_list[k].count(mey[m],0,len(kural_list[k])-1)
        all_count = all_count + this_count
    mey_count.append([mey[m],all_count])
print(mey_count)
[['க்', 976], ['ங்', 261], ['ச்', 184], ['ஞ்', 189], ['ட்', 273], ['ண்', 692], ['த்', 708], ['ந்', 595], ['ப்', 588], ['ம்', 1735], ['ய்', 383], ['ர்', 1187], ['ல்', 1308], ['வ்', 49], ['ழ்', 141], ['ள்', 431], ['ற்', 808], ['ன்', 2005]]
In [35]:
umey = np.reshape(np.array(uyirmey),(18,-1))
In [36]:
umey
Out[36]:
array([['க', 'கா', 'கி', 'கீ', 'கு', 'கூ', 'கெ', 'கே', 'கை', 'கொ', 'கோ',
        'கௌ'],
       ['ங', 'ஙா', 'ஙி', 'ஙீ', 'ஙு', 'ஙூ', 'ஙெ', 'ஙே', 'ஙை', 'ஙொ', 'ஙோ',
        'ஙௌ'],
       ['ச', 'சா', 'சி', 'சீ', 'சு', 'சூ', 'செ', 'சே', 'சை', 'சொ', 'சோ',
        'சௌ'],
       ['ஞ', 'ஞா', 'ஞி', 'ஞீ', 'ஞு', 'ஞூ', 'ஞெ', 'ஞே', 'ஞை', 'ஞொ', 'ஞோ',
        'ஞௌ'],
       ['ட', 'டா', 'டி', 'டீ', 'டு', 'டூ', 'டெ', 'டே', 'டை', 'டொ', 'டோ',
        'டௌ'],
       ['ண', 'ணா', 'ணி', 'ணீ', 'ணு', 'ணூ', 'ணெ', 'ணே', 'ணை', 'ணொ', 'ணோ',
        'ணௌ'],
       ['த', 'தா', 'தி', 'தீ', 'து', 'தூ', 'தெ', 'தே', 'தை', 'தொ', 'தோ',
        'தௌ'],
       ['ந', 'நா', 'நி', 'நீ', 'நு', 'நூ', 'நெ', 'நே', 'நை', 'நொ', 'நோ',
        'நௌ'],
       ['ப', 'பா', 'பி', 'பீ', 'பு', 'பூ', 'பெ', 'பே', 'பை', 'பொ', 'போ',
        'பௌ'],
       ['ம', 'மா', 'மி', 'மீ', 'மு', 'மூ', 'மெ', 'மே', 'மை', 'மொ', 'மோ',
        'மௌ'],
       ['ய', 'யா', 'யி', 'யீ', 'யு', 'யூ', 'யெ', 'யே', 'யை', 'யொ', 'யோ',
        'யௌ'],
       ['ர', 'ரா', 'ரி', 'ரீ', 'ரு', 'ரூ', 'ரெ', 'ரே', 'ரை', 'ரொ', 'ரோ',
        'ரௌ'],
       ['ல', 'லா', 'லி', 'லீ', 'லு', 'லூ', 'லெ', 'லே', 'லை', 'லொ', 'லோ',
        'லௌ'],
       ['வ', 'வா', 'வி', 'வீ', 'வு', 'வூ', 'வெ', 'வே', 'வை', 'வொ', 'வோ',
        'வௌ'],
       ['ழ', 'ழா', 'ழி', 'ழீ', 'ழு', 'ழூ', 'ழெ', 'ழே', 'ழை', 'ழொ', 'ழோ',
        'ழௌ'],
       ['ள', 'ளா', 'ளி', 'ளீ', 'ளு', 'ளூ', 'ளெ', 'ளே', 'ளை', 'ளொ', 'ளோ',
        'ளௌ'],
       ['ற', 'றா', 'றி', 'றீ', 'று', 'றூ', 'றெ', 'றே', 'றை', 'றொ', 'றோ',
        'றௌ'],
       ['ன', 'னா', 'னி', 'னீ', 'னு', 'னூ', 'னெ', 'னே', 'னை', 'னொ', 'னோ',
        'னௌ']], 
      dtype='<U2')
In [37]:
len(umey) 
Out[37]:
18
In [38]:
np.shape(umey)
Out[38]:
(18, 12)
In [39]:
umey_count = []
umey_count_all = []
for m in range(0,18):
    for n in range(0,12):
        all_count=0
        for k in range(0,1330):
            this_count = kural_list[k].count(umey[m][n],0,len(kural_list[k])-1)
            if n == 0:
                x_count = 0
                for x in range(1,12):
                    count = kural_list[k].count(umey[m][x],0,len(kural_list[k])-1)
                    x_count = x_count + count
                this_count = this_count - x_count
                y_count = kural_list[k].count(mey[m],0,len(kural_list[k])-1)
                this_count = this_count - y_count
            umey_count.append([n+1,m+1,uyir[n],mey[m],umey[m][n],this_count])
            all_count = all_count + this_count
        umey_count_all.append([n+1,m+1,uyir[n],mey[m],umey[m][n],all_count])
In [40]:
len(umey_count_all)
Out[40]:
216
In [41]:
umey_count_all[0:5]
Out[41]:
[[1, 1, 'அ', 'க்', 'க', 1255],
 [2, 1, 'ஆ', 'க்', 'கா', 400],
 [3, 1, 'இ', 'க்', 'கி', 199],
 [4, 1, 'ஈ', 'க்', 'கீ', 13],
 [5, 1, 'உ', 'க்', 'கு', 641]]
In [42]:
pd_umey_count_all = pd.DataFrame(umey_count_all)
In [43]:
pd_umey_count_all.columns = ['u_num','m_num','uyir','mey','uyirmey','count']
pd_umey_count_all.head()
Out[43]:
u_num m_num uyir mey uyirmey count
0 1 1 க் 1255
1 2 1 க் கா 400
2 3 1 க் கி 199
3 4 1 க் கீ 13
4 5 1 க் கு 641
In [44]:
y = np.array(uyir_count)[:,1].astype(np.float)/10
y
Out[44]:
array([ 83.4,  20.4,  67. ,   4.9,  58.4,   7.1,  45.1,   6.5,   1.9,
        20.1,   3.2,   0. ])
In [45]:
import plotly as py
import plotly.graph_objs as go
py.offline.init_notebook_mode()

trace0 = go.Scatter(
    x= list(np.array(uyir_count)[:,0]),
    y= list(np.array(uyir_count)[:,1]),
    text = list(np.array(uyir_count)[:,0]),
    mode='markers',
    marker=dict(
        size= list(np.array(uyir_count)[:,1].astype(np.float)/10),
        color = list(np.array(uyir_count)[:,1])
    )
)

data = [trace0]
py.offline.iplot(data, filename='bubblechart-size')
In [46]:
import plotly as py
import plotly.graph_objs as go
py.offline.init_notebook_mode()

trace0 = go.Scatter(
    x= list(np.array(mey_count)[:,0]),
    y= list(np.array(mey_count)[:,1]),
    text = list(np.array(mey_count)[:,0]),
    mode='markers',
    marker=dict(
        size= list(np.array(mey_count)[:,1].astype(np.float)/10),
        color = list(np.array(mey_count)[:,1])
        )
)

data = [trace0]
py.offline.iplot(data, filename='bubblechart-size')
In [47]:
import plotly as py
from plotly.graph_objs import *
py.offline.init_notebook_mode()

import pandas as pd

# Get Data: this ex will only use part of it (i.e. rows 750-1500)


trace1 = Scatter3d(
    x= pd_umey_count_all['uyir'],
    y= pd_umey_count_all['mey'],
    z= pd_umey_count_all['count'],
    
    text=pd_umey_count_all['uyirmey'],
    mode='markers',
    marker=dict(
        sizemode='diameter',
        sizeref=7,
        size=pd_umey_count_all['count'],
        color = pd_umey_count_all['count'],
        colorscale = 'Viridis',
        colorbar = dict(title = 'Uyirmey Count <br> by Kural'),
        opacity = 0.5   
    )
)

data=[trace1]
layout=dict(height=800, width=800, title="Scatter plot of 'uyirmey' character count by each Thirukkural")
fig=dict(data=data, layout=layout)
py.offline.iplot(fig, filename='3DBubble')
In [ ]: